<?php
class HotelsController extends AppController
{
    var $uses = array('Hotel', 'Location');
    var $paginate = array(
        'limit' => 20,
        'order' => array(
            'Hotel.name' => 'ASC'
        )
    );

    function index()
    {
        if (isset($this->params['named']['location_id']))
        {
            $this->data['Hotel']['location_id'] = $this->params['named']['location_id'];
        }
        
        if (isset($this->params['named']['query']))
        {
            $query = $this->params['named']['query'];
            //$query = htmlspecialchars_decode($query);
            $this->set('query', $query);

            $conditions = array();
            //$conditions['Customer.information LIKE'] = '%' . mysql_real_escape_string(trim($query)) . '%';
            $query_string = explode(',', $query);
            $count = 1;
            foreach ($query_string as $search_string)
            {
                $conditions[] = array('Hotel.name LIKE' => "%" . mysql_real_escape_string(trim($search_string)) . "%");
                $count++;
            }

            if ($this->data['Hotel']['location_id'] != 'all' && is_numeric($this->data['Hotel']['location_id']))
            {
                $conditions[] =  array('Hotel.location_id' => $this->data['Hotel']['location_id']);
            }

            $this->paginate = array(
                'limit' => 20,
                'order' => array(
                    'Hotel.created' => 'desc'
                ),
                'conditions' => $conditions,
                //'conditions' => $conditions
            );
        }

        $conditions = array();
        if ($this->data['Hotel']['location_id'] != 'all' && is_numeric($this->data['Hotel']['location_id']))
        {
            $conditions[] =  array('Hotel.location_id' => $this->data['Hotel']['location_id']);
        }

        $all_hotels = $this->Hotel->find('all', array('conditions' => $conditions));
        $this->set('all_hotels', $all_hotels);

        $locations['all'] = 'Tất cả';
        $locations += $this->Location->find('list');
        $this->set('locations', $locations);
        
        $hotels = $this->paginate('Hotel');
        $this->set('hotels', $hotels);
    }

    function add()
    {
        if (!empty($this->data))
        {
            $new_location = false;
            if (!empty($this->data['Location']['name']))
            {
                $location = $this->Location->findByName($this->data['Location']['name']);
                if ($location)
                {
                    $this->data['Hotel']['location_id'] = $location['Location']['id'];
                }
                else
                {
                    if ($this->Location->save($this->data['Location']))
                    {
                        $new_location = true;
                        $this->data['Hotel']['location_id'] = $this->Location->id;
                    }
                }   
            }

            if ($this->Hotel->save($this->data))
            {
                if ($new_location)
                {
                    $this->flash('Thêm Khách sạn và Địa điểm thành công!', '');
                }
                else
                {
                    $this->flash('Thêm Khách sạn thành công!', '');
                }
            }
        }
        $locations = $this->Location->find('list');
        $this->set('locations', $locations);
    }

    function edit($id)
    {
        $this->Hotel->id = $id;
        if (empty($this->data))
        {
            $this->data = $this->Hotel->read();
            $this->data['Location']['name'] = null;
        }
        else
        {
            $new_location = false;
            if (!empty($this->data['Location']['name']))
            {
                $location = $this->Location->findByName($this->data['Location']['name']);
                if ($location)
                {
                    $this->data['Hotel']['location_id'] = $location['Location']['id'];
                }
                else
                {
                    if ($this->Location->save($this->data['Location']))
                    {
                        $new_location = true;
                        $this->data['Hotel']['location_id'] = $this->Location->id;
                    }
                }
            }
            
            if ($this->Hotel->save($this->data))
            {
                if ($new_location)
                {
                    $this->flash('Sửa Khách sạn và thêm Địa điểm thành công!', '');
                }
                else
                {
                    $this->flash('Sửa Khách sạn thành công!', '');
                }
            }
        }
        $locations = $this->Location->find('list');
        $this->set('locations', $locations);
    }

    function delete($id)
    {
        $this->Hotel->delete($id);
        $this->redirect(array('controller' => 'hotels', 'action' => 'index'));
    }
}
?>
